#include <windows.h>
#include <tchar.h>
#include "resource.h"

HINSTANCE ghInstance;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
INT_PTR CALLBACK DlgModalProc(HWND hDlg, UINT nMessage, WPARAM wParam, LPARAM lParam)
{
	switch (nMessage)
	{
		case WM_INITDIALOG:
			CreateWindow(_T("Button"), _T("Close Dlg"), WS_CHILD|WS_VISIBLE, 0, 120, 100, 50, hDlg, (HMENU)1002, NULL, 0);
			break;
		case WM_COMMAND:
			EndDialog(hDlg, 0);
			break;
	}
	return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
INT_PTR CALLBACK DlgModalLessProc(HWND hDlg, UINT nMessage, WPARAM wParam, LPARAM lParam)
{
	switch (nMessage)
	{
		case WM_INITDIALOG:
			CreateWindow(_T("Button"), _T("Close Dlg"), WS_CHILD|WS_VISIBLE, 0, 120, 100, 50, hDlg, (HMENU)1002, NULL, 0);
			break;
		case WM_COMMAND:
			DestroyWindow(hDlg);
			break;
	}
	return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT nMessage, WPARAM wParam, LPARAM lParam)
{
	HWND hWnd;
	switch (nMessage)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Demo0053"));
			CreateWindow(_T("Button"), _T("Model Dlg"), WS_CHILD|WS_VISIBLE, 0, 0, 100, 50, hDlg, (HMENU)1000, NULL, 0);
			CreateWindow(_T("Button"), _T("Modaless Dlg"), WS_CHILD|WS_VISIBLE, 0, 60, 100, 50, hDlg, (HMENU)1001, NULL, 0);
			CreateWindow(_T("Button"), _T("Close Dlg"), WS_CHILD|WS_VISIBLE, 0, 120, 100, 50, hDlg, (HMENU)1002, NULL, 0);
			break;
		}
		case WM_COMMAND:
		{
			switch (wParam)
			{
				case 1000:		// create modal dialog
				{
					DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_DLGMODAL), hDlg, DlgModalProc, NULL);
					break;
				}
				case 1001:		// create modalless dialog
				{
					hWnd = CreateDialog(ghInstance, MAKEINTRESOURCE(IDD_DLGMODALLESS), hDlg, DlgModalLessProc);
					ShowWindow(hWnd, SW_SHOW);
					UpdateWindow(hWnd);
					break;
				}
				case 1002:		// close dialog
				{
					EndDialog(hDlg, 0);
					break;
				}
			}
			break;
		}
	}
	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
	ghInstance = hInstance;
	
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOGBAR), NULL, DlgProc);

	return TRUE;
}